<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <#include "../common/include_easyui.ftl">
    <#assign ctx = request.contextPath>
    <title>用户列表</title>

    <script language="javascript">
        var ctxPath = "${ctx}";
        jQuery(function () {

            //加载部门列表
            var qry_department_tag = 0;
            $('#qry_department').combobox({
                url: ctxPath+'/userMgrController/loadDepartmentList',
                valueField: 'id',
                textField: 'text',
                label: '所属部门：',
                labelAlign: 'right',
                editable: false,
                labelPosition: 'left',
                onLoadSuccess: function (data) {

                    if (qry_department_tag == 0) {
                        data.unshift(0, {"id": -1, "text": "==全部=="});
                    }
                    qry_department_tag++;
                    if (qry_department_tag == 1) {
                        $('#qry_department').combobox("loadData", data);
                        $('#qry_department').combobox("setValue", -1);
                    }

                }

            });

            var toolBarArray = [
                {
                    id: "search", text: "查询数据", iconCls: "icon-search", handler: function () {
                        //组装json数据
                        var qryAccount = jQuery("#qry_account").val();
                        var qryName = jQuery("#qry_name").val();
                        var qryUserStatus = jQuery("#qry_user_status").combobox("getValue");
                        var qryDepartmentId = jQuery("#qry_department").combobox("getValue");
                        if (qryDepartmentId == -1) {
                            qryDepartmentId = null;
                        }

                        var paramObj = {
                            "qryAccount": qryAccount,
                            "qryName": qryName,
                            "qryDepartmentId": qryDepartmentId,
                            "qryUserStatus": qryUserStatus
                        };
                        jQuery("#div_table").datagrid({
                            queryParams: paramObj
                        });
                        jQuery("#div_table").datagrid("reload");
                    }
                },
                {
                    id: "add", text: "添加用户", iconCls: "icon-add", handler: function () {
                        addFun();
                    }
                },
                {
                    id: "edit", text: "修改用户", iconCls: "icon-edit", handler: function () {
                        editFun();
                    }
                },
                {
                    id: "delete", text: "删除用户", iconCls: "icon-remove", handler: function () {
                        deleteFun();
                    }
                },
                {id: "export", text: "导出数据", iconCls: "icon-redo"},
                {
                    id: "reoload", text: "刷新数据", iconCls: "icon-reload", handler: function () {
                        jQuery("#div_table").datagrid("reload");
                    }
                }
            ];

            jQuery("#div_table").datagrid({
                //字段的属性
                frozenColumns: [
                    [
                        {field: 'checkedId', checkbox: true},
                        {field: 'userId', title: '用户ID', width: 100, halign: "center", align: "center", sortable: true},
                        {field: 'name', title: '用户姓名', width: 100, halign: "center", align: "center", sortable: true},
                        {
                            field: 'userAccount',
                            title: '用户账号',
                            width: 120,
                            halign: "center",
                            align: "center",
                            sortable: true
                        },
                        {field: "department", title: '所属部门', width: 120, halign: "center", align: "center"}
                    ]
                ],

                columns: [
                    [
                        {field: "jobTitle", title: '职称', width: 150, halign: "center", align: "center"},
                        {
                            field: "userStatus", title: '用户状态', width: 150, halign: "center", align: "center",
                            formatter: function (value, row, index) {
                                if (value == "1") {
                                    return "<font color='green'>正常</font>";
                                } else {
                                    return "<font color='red'>离职</font>";
                                }
                            }
                        },
                        {
                            field: "sex", title: '性别', width: 80, align: "center",
                            formatter: function (value, row, index) {
                                if (value == "0") {
                                    return "保密";
                                } else if (value == "1") {
                                    return "<font color='blue'>男</font>";
                                } else {
                                    return "<font color='red'>女</font>";
                                }
                            }
                        },
                        {
                            field: "maritalStatus", title: '婚姻状况', width: 100, halign: "center", align: "center",
                            formatter: function (value, row, index) {
                                if (value == "0") {
                                    return "保密";
                                } else if (value == "1") {
                                    return "<font color='blue'>已婚</font>";
                                } else {
                                    return "<font color='red'>未婚</font>";
                                }
                            }
                        },
                        {field: "createdTime", title: '创建时间', width: 200, halign: "center", align: "center"},
                        {field: "officeTel", title: '办公电话', width: 150, halign: "center", align: "center"},
                        {field: "phone", title: '手机号', width: 150, halign: "center", align: "center"},
                        {field: "email", title: '电子邮箱', width: 200, halign: "center", align: "center"},
                        {field: "idCardNo", title: '身份证号', width: 200, halign: "center", align: "center"},
                        {field: "nativePlace", title: '籍贯', width: 150, halign: "center", align: "center"},
                        {field: "address", title: '地址', width: 300, halign: "center", align: "left"}
                    ]
                ],
                //加载网格的数据
                url: ctxPath + "/userMgrController/userListJson?date=" + new Date() + "",
                //网格属性的设置
                fit: true,
                rownumbers: true,
                ctrlSelect: true,
                striped: true,
                resizable: true,
                toolbar: toolBarArray,
                //分页的属性
                pagination: true,
                pageList: [5, 8, 10, 15],
                pageSize: 8,
                //对表格进行排序
                remoteSort: true
            });

            function addFun() {
                //模态对话框
                var addURL = ctxPath + "/userMgrController/preAdd";

                jQuery('#dialog_div').dialog({
                    title: "新增用户",
                    width: 700,
                    height: 500,
                    closed: false,
                    cache: false,
                    modal: true,
                    content: "<iframe id=\"testIFrame\" src='" + addURL + "' width='99%' height='98%' scrolling='auto'></iframe>",
                    buttons: [
                        {
                            id: "saveButton", text: "保存数据", iconCls: "icon-save", handler: function () {
                                callChild_saveData();
                            }
                        },
                        {
                            id: "closeButton", text: "关闭窗口", iconCls: "icon-cancel", handler: function () {
                                jQuery('#dialog_div').dialog("close");
                            }
                        }
                    ]
                });
            }

            function editFun() {
                //Dialog对话框
                var selectRows = jQuery("#div_table").datagrid('getSelections');
                if (selectRows.length == 0) {
                    jQuery.messager.alert('提示', '修改操作必须选择一行数据', 'warning');
                } else if (selectRows.length > 1) {
                    jQuery.messager.alert('提示', '修改操作只能选择一行数据，请重新选择', 'warning');
                    jQuery("#div_table").datagrid('clearSelections');
                } else {
                    var userId = selectRows[0].userId;

                    var editURL = ctxPath + "/userMgrController/preModify/" + userId + "";
                    jQuery('#dialog_div').dialog({
                        title: "修改用户",
                        width: 700,
                        height: 500,
                        closed: false,
                        cache: false,
                        modal: true,
                        //href: 		ctxPath+'/easyui/panel_01.jsp'		//只能加载页面中非javascript的内容。
                        content: "<iframe id=\"testIFrame\" src='" + editURL + "' width='99%' height='98%' scrolling='auto'></iframe>",
                        buttons: [
                            {
                                id: "saveButton", text: "保存数据", iconCls: "icon-save", handler: function () {
                                    callChild_saveData();
                                }
                            },
                            {
                                id: "closeButton", text: "关闭窗口", iconCls: "icon-cancel", handler: function () {
                                    jQuery('#dialog_div').dialog("close");
                                }
                            }
                        ]
                    });
                }
            }

            function deleteFun() {

                var selectRows = jQuery("#div_table").datagrid('getSelections');

                if (selectRows.length == 0) {
                    jQuery.messager.alert('提示', '删除操作至少选择一行数据', 'warning');
                } else {

                    $.messager.confirm('删除提示', '您确定要删除选中的用户吗？', function (flag) {
                        if (flag) {
                            var idArray = new Array();
                            for (var i = 0; i < selectRows.length; i++) {
                                var userId = selectRows[i].userId;
                                idArray.push(userId);
                            }
                            var decUserIds = idArray.join(",");

                            var decURL = ctxPath + "/userMgrController/deleteUser?date=" + new Date() + "";

                            var paramObj = {'decUserIds': decUserIds};

                            jQuery.post(decURL, paramObj, function (responseData) {

                                var code = responseData.code;

                                if (code == "200") {
                                    $.messager.alert('我的消息', '用户信息删除成功！', 'info', function () {
                                        jQuery("#div_table").datagrid("reload");
                                    });

                                } else {
                                    $.messager.alert('我的消息', "数据删除出错，原因 ：" + responseData.message, 'error');
                                }

                            }, "json");
                        }
                    });


                }
            }

            function callChild_saveData() {
                var testIFrame = jQuery("#testIFrame")[0];
                var childWin = testIFrame.contentWindow;
                childWin.saveData();
            }
        });
    </script>

</head>
<body>
<body class="easyui-layout">
<div data-options="region:'north',title:'查询条件'" style="height:80px;padding-left:10px;padding-top:15px;background:#eee;">
    <input class="easyui-textbox" name="qry_name" id="qry_name" style="width:30%;max-width:270px;" labelAlign="right"
           data-options="label:'姓名：'">
    <input class="easyui-textbox" name="qry_account" id="qry_account" style="width:30%;max-width:270px;"
           labelAlign="right" data-options="label:'账户：'">&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="qry_department" id="qry_department" style="width:30%;max-width:260px;">&nbsp;&nbsp;&nbsp;&nbsp;
    <select class="easyui-combobox" id="qry_user_status" label="用户状态：" labelPosition="left" labelAlign="right"
            data-options="editable:false" style="width:30%;max-width:260px;">
        <option value="1" selected="selected">正常</option>
        <option value="0">离职</option>
    </select>
</div>
<div data-options="region:'center'" style="padding:0px;background:#eee;">
    <div id="div_table"></div>
</div>
<div id="dialog_div"></div>
</body>
</html>